﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class Admin_Default : System.Web.UI.Page
{
    NhanVien nv = new NhanVien();
    DataConnection dt = new DataConnection();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            if (Session["user"] != null)
            {
                MultiView1.SetActiveView(View3);
                GridView1.DataSource = nv.LayDSNhanVien();
                GridView1.DataBind();
            }
            else
                Response.Redirect("DangNhap.aspx");
        }
            
    }
    protected void Button11_Click(object sender, EventArgs e)
    {
        bool gt = true;
        if (RadioButtonList1.Items[0].Selected == true)
            gt = true;
        else
            gt = false;
        GridView1.DataSource =nv.TimNV (TextBox16.Text,gt);
        GridView1.DataBind();
    }
    protected void Button12_Click(object sender, EventArgs e)
    {
        MultiView1.SetActiveView(View1);
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        GridView1.DataSource = nv.LayDSNhanVien();
        GridView1.DataBind();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataSource = nv.LayDSNhanVien();
        GridView1.DataBind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        GridView1.DataSource = nv.LayDSNhanVien();
        GridView1.DataBind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int MaNV=int.Parse(GridView1.Rows[e.RowIndex].Cells[1].Text);

        TextBox txtHoten = (TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0];
        string hoten = txtHoten.Text;

        TextBox txtTenDN = (TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0];
        string tendn = txtTenDN.Text;

        TextBox txtMatkhau = (TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0];
        string matkhau = txtMatkhau.Text;

        RadioButtonList rb = (RadioButtonList)GridView1.Rows[e.RowIndex].Cells[5].FindControl("RadioButtonList3");
        bool gt = true;
        if(rb.Items[0].Selected==true)
            gt = true;
        else
            gt = false;
        
        TextBox txtngaysinh = (TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0];
        string ngaysinh = txtngaysinh.Text;


        TextBox txtdiachi = (TextBox)GridView1.Rows[e.RowIndex].Cells[7].Controls[0];
        string diachi = txtdiachi.Text;

        TextBox txtemail = (TextBox)GridView1.Rows[e.RowIndex].Cells[8].Controls[0];
        string email = txtemail.Text;

        TextBox txtdienthoai = (TextBox)GridView1.Rows[e.RowIndex].Cells[9].Controls[0];
        string dienthoai = txtdienthoai.Text;


        TextBox txtcmnd = (TextBox)GridView1.Rows[e.RowIndex].Cells[10].Controls[0];
        string cmnd = txtcmnd.Text;

        nv.SuaNV(MaNV, hoten, tendn, matkhau, gt, ngaysinh, diachi, email, dienthoai, cmnd);

        GridView1.EditIndex = -1;
        GridView1.DataSource = nv.LayDSNhanVien();
        GridView1.DataBind();
    }
    protected void Button14_Click1(object sender, EventArgs e)
    {
        bool gt = true;
        if (RadioButtonList2.Items[0].Selected == true)
            gt = true;
        else
            gt = false;
        nv.InsertNV(TextBox6.Text, TextBox19.Text, TextBox20.Text,gt,TextBox9.Text, TextBox10.Text, TextBox11.Text, TextBox12.Text, TextBox17.Text);
        Label2.Text = "Thêm thành công.";
    }
     protected void Button15_Click(object sender, EventArgs e)
    {
        try
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox chb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
                if (chb.Checked == true)
                {
                    int ma = Convert.ToInt32(GridView1.Rows[i].Cells[1].Text);
                    nv.XoaNV(ma);
                }
            }
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;

        }
        GridView1.DataSource = nv.LayDSNhanVien();
        GridView1.DataBind();
    }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        //SqlParameter p1 = new SqlParameter("@ten", args.Value);
        //string sql = "select TenDN from NhanVien where TenDN=@ten";
        //DataTable tbl = dt.GetData(sql, p1);
        //if (tbl.Rows.Count > 0)
        //    args.IsValid = false;
        //else
        //    args.IsValid = true;
    }
}
